package com.xm.service.controller;


import com.xm.service.framework.util.ImageUtil;
import com.xm.service.framework.util.Result;
import com.xm.service.module.USER.po.User;
import com.xm.service.module.USER.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserController {

    protected String str;
    @Autowired
    private UserService userService;

    /**
     * 根据用户名和密码进行登陆验证，并返回登录后的User对象。如果验证成功，则返回200 OK和User对象；否则返回401 Unauthorized和错误信息。
     */
    @PostMapping("/login")
    public Result login(@RequestParam int uid, @RequestParam String pwd) {
        User user = userService.login(uid,pwd); // 调用 UserService 的 login() 方法进行登陆验证
        if(user!=null){
            return Result.success(user,"登录成功！");
        }else{
            return Result.error("123","账号或密码错误！");
        }
    }

    @PostMapping("/register")
    public Result register (@RequestParam String uname,@RequestParam String password,
                            @RequestParam String email,@RequestParam int age,
                            @RequestParam String gender){
        Result result = userService.creatorUser(uname,password,email,age,gender);
        System.out.println(result.getData());

        if(result.getData()==null){
            return Result.error("notion",result.getMsg());
        }else{
            User user=(User)result.getData();
            if(result.getCode() == "0"){
                return Result.success(user,"注册成功");
            }else{
                return Result.error("notion", result.getMsg());
            }//这里有很大问题
        }
    }

    @RequestMapping("/verifyCode")//验证码
    public byte[] generateImage() throws IOException{
        ImageUtil imageUtil = ImageUtil.getInstance();
        ByteArrayInputStream image = imageUtil.getImage();
        this.str = imageUtil.getStr();
        System.out.println("正确验证码："+str);
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];
        int len;
        try{
            while ((len = image.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            image.close();
            out.flush();
            return out.toByteArray();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/checkCode")
    public int checkCode(@RequestParam String code){
        System.out.println("前端:"+code);
        code = code.toLowerCase();
        String right = this.str.toLowerCase();
        if(code.equals(right)){
            return 1;//如果正确，则为1
        }else{
            return 0;
        }

    }


}
